package com.sc.nft.task;

import cn.hutool.core.collection.CollUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.service.BlockChainTypeService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;


@Component
@RequiredArgsConstructor
@Slf4j
public class UserInitTask {


    private final BlockChainTypeService blockChainTypeService;

    private final UserInfoDao userInfoDao;
    private final UserCollectionDao userCollectionDao;

    private final BoxTakePoolDao boxTakePoolDao;

    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;

    private final ActivityPropsDao activityPropsDao;
    private final UserEquityPropsDao userEquityPropsDao;

    /**
     * 用户初始化 钱包/链地址
     *
     * @throws Exception
     */
    @XxlJob("intUserAssets")
    public void intUserAssets() throws Exception {


        List<UserInfo> list = userInfoDao.findListByIsAddr();

        for (UserInfo info : list) {
            blockChainTypeService.insertUserAssets(info.getId());
        }
    }

    /**
     * 用户初始化 钱包
     *
     * @throws Exception
     */
    @XxlJob("intUserWallet")
    public void intUserWallet() throws Exception {

//        List<UserInfo> list = userInfoDao.list();
//
//        for (UserInfo info : list) {
//            blockChainTypeService.intUserWallet(info.getId());
//        }

        log.warn("该方法已废弃，禁止一次性查出全部用户方法");
    }


    /**
     * 初始化用户已兑换藏品的兑换活动ID
     */
    @XxlJob("initUserCollection")
    public void initUserCollection() throws InterruptedException {
        List<BoxTakePool> takePools = boxTakePoolDao.getUpperActivity(2);
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(8);
        for (BoxTakePool pool : takePools) {
            XxlJobHelper.log("当前初始化的兑换活动，task_pool_id:{}" + pool.getId());
            DigitalCollectionProductInfo collectionProductInfo = digitalCollectionProductInfoDao.getById(pool.getProductBoxId());
            if(Objects.isNull(collectionProductInfo)){
                continue;
            }
            Long id = 0L;
            do {
                List<UserCollection> userCollections = userCollectionDao.queryByProductIdForInit(collectionProductInfo.getRelationId(), id, 80000, Boolean.TRUE);
                if (userCollections.isEmpty()) {
                    break;
                }
                List<List<UserCollection>> split = CollUtil.split(userCollections, userCollections.size() / 8);
                for (List<UserCollection> collections : split) {
                    threadPoolExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            XxlJobHelper.log("线程{}开始工作", Thread.currentThread().getId());
                            userCollectionDao.initUserCollection(collections, pool.getId());
                        }
                    });
                }
                //等待三秒
                Thread.sleep(3000);
                //仍然有活动的线程和队列停顿10s
                while (threadPoolExecutor.getActiveCount() != 0 && threadPoolExecutor.getQueue().size() != 0) {
                    Thread.sleep(10000);
                }
                id = userCollections.get(userCollections.size() - 1).getId();
            } while (true);

        }

    }


    /**
     * 初始化用户已兑换心愿盒的兑换活动ID
     */
    @XxlJob("initUserBox")
    public void initUserBox() throws InterruptedException {
        List<BoxTakePool> takePools = boxTakePoolDao.getUpperActivity(1);
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(8);
        for (BoxTakePool pool : takePools) {
            XxlJobHelper.log("当前初始化的心愿盒兑换活动，task_pool_id:{}" + pool.getId());
            DigitalCollectionProductInfo collectionProductInfo = digitalCollectionProductInfoDao.getById(pool.getProductBoxId());
            if(Objects.isNull(collectionProductInfo)){
                continue;
            }
            Long id = 0L;
            do {
                List<UserCollection> userCollections = userCollectionDao.queryByProductIdForInit(collectionProductInfo.getRelationId(), id, 80000, Boolean.FALSE);
                if (userCollections.isEmpty()) {
                    break;
                }
                List<List<UserCollection>> split = CollUtil.split(userCollections, userCollections.size() / 8);
                for (List<UserCollection> collections : split) {
                    threadPoolExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            XxlJobHelper.log("线程{}开始工作", Thread.currentThread().getId());
                            userCollectionDao.initUserCollection(collections, pool.getId());
                        }
                    });
                }
                //等待三秒
                Thread.sleep(3000);
                //仍然有活动的线程和队列停顿10s
                while (threadPoolExecutor.getActiveCount() != 0 && threadPoolExecutor.getQueue().size() != 0) {
                    Thread.sleep(10000);
                }
                id = userCollections.get(userCollections.size() - 1).getId();
            } while (true);

        }

    }

}
